package com.nj.liandi.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.nj.liandi.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/** description: 支付宝支付的模板blsngo0026@sandbox.com
 * version: 1.0 
 * date: 2021/11/8 10:51
 * */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118647366";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbYeG3lAxAgxJ2iRf2rrFLAZf4rJe4uH5QGamaD6bM4rAKCLwzIBPzPSRv+NN29M2nzeF35FZPTZ1CegZhD8jGZQWrJZc20Qn6yf4MCqwR4Jky51KpUAuatyayWI5EZXNOUzYDZsYBHHiUQPOTsc9CMFnxM6OFSIL2D0pZ55YsYiBztzxPxaNawrNDGNpqoHcIgNBlquQ9pUyZJZoGnUJXMhKO1i+5oy6XHowRC+ztdDdviVH9Fy2/xJDzdoRQs7X3wO3Lq/m77x5dj9ZAv74EIa+xGjnh3ceGLqQFW2DZ4Hr7R1rjOQYZfbcgYZV9NLxD7r3yPuA3vh7LbnAbL033AgMBAAECggEAPXh4uo6YVkFEYZ6U22N7AkhsHdXx5AMg/YFZhnhgrbeUmzOfeL5dBiKPMrkGt43bemJJq4uq1Y7IAw+RTH7ZGBvJJU8mxmnmf8+/cHYcYqovEWNQYWkoXhnM9ktwZQCZoEftG2ViOfhQf54+fpSegPnt6y1ZqJXqMXwVFwI2d/HvDQxTGWuItRfUKwcXEmhYyCzxcaBG3Ij5Q2BTF3ZtYFJ29NSI68sUFnAm5rskuHfFTRfx+8bj6YeHW/akpHrn5RkG4rrBdXvELPqbhArfXxuqA8LmrasGgF8Yp6d0OuH2OZ14lpJDk4Tae2ylJ5LL+Qiq6GD+ocEotHZnbPlfGQKBgQDhyoj4RG9hq0ItW+wxnCCagQeCGsDS7AO1Znll743HRQaG5hmXYlx42piewcrKoOsBJjEgas14czBVRfhIW0PJXiWA15tMS36igjiBHYRv6r+1CCjbLRkHG/EbbRtx+w1gIEo+mHwDyaQuuouQ7Iai2MVey5CW6FM3OR3VKMACSwKBgQCwK87zqeAiVvUrozC06r6Wm2v618cegyFuACv/FtaPN1ZB1tAv83iYCmLci36YKtJcKf4VeR7jZWhQsb+sGuImwdL+9fbnoiwAgUYWgE8RiHGZMNFu+P/+QtPsHbGw8ClIdQi4Xi5y0652g3JOQvkgH+gOeycc5mavE7N5NtA3hQKBgQDf3YGkNaYBPl2kv2kVXujsh1W8Z24bMioTJwDiUntNwWBRS76/26Q3pvR/I0E/WgDm6Mj4jSwfaoXXZkJe6y3wb0nbzumI4aky0s3XJj7y/9sHUInBlZsk801AlhMKFZITZrB2E5WR49DHC+nozLpIp4g5DrfTB5HKRPURLRACtQKBgG9fwr4YrzIGd4jW6nv1y/e62lEDCUco74Fzal0iOxLfkRCelY87BQpZxpZXOflJWSQbCB7bK8Pm4EPjm6FafY6zColVRnq588d98qlXBKW49Ng8HKtjj7NW3FNxklyh9/duxcyI/iRp3undv+3qOUUt5q8A/IEwx8n3UodXN9thAoGAb2mw3n/FmD1oj3hHzNLt5q/6KFr4ornAHP7O/qJueeyGCbOs440KcsREO7kFCDyw+NaEgoDwCBH3UGq2Re6/14mgG1Z64o1j9Sh3XxlNhWAJG4RacFdD9bfnZHu+ATw+lGZS9zQW4k3Z2kZ5GZXL2GnfcTPukmCAl//mfPl8Ckk=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr6jv1TAUEJUIcNFGw8XUfyMmqoHPaulLMnRXCgJ1oMXkmjuBcymNm49TWD/DiF3I+oDLM02j9R39kf+YIhVOyzVj7fjq9KNENToojAO5RRZbru13boVFLH/v6P9byjdj7yQ15vx5IInNVT4M06k0mNYstmd8xDdfDZC0FUNFbVdEGwXOwGnW3sB+DYv5q6hDZTcOdlFbJbgYj0ADsKr6zd/9+ws/Oe/EbuiR5RsTWnO1DpBRsoYZm5eY/pdNLF2+ViKr+oTBaEPEiRkt66ms15EScQBDz8aNHyED/nnnj6IgDt53NJNmpZH7MBa6mjgdfFN27UMNZG6GfT28RgLG0wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url ="http://2gybt86lll.51xd.pub/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //支付超时时间
    private String time_out = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ time_out +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }

    public String getApp_id() {
        return app_id;
    }

    public void setApp_id(String app_id) {
        this.app_id = app_id;
    }

    public String getMerchant_private_key() {
        return merchant_private_key;
    }

    public void setMerchant_private_key(String merchant_private_key) {
        this.merchant_private_key = merchant_private_key;
    }

    public String getAlipay_public_key() {
        return alipay_public_key;
    }

    public void setAlipay_public_key(String alipay_public_key) {
        this.alipay_public_key = alipay_public_key;
    }

    public String getNotify_url() {
        return notify_url;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getReturn_url() {
        return return_url;
    }

    public void setReturn_url(String return_url) {
        this.return_url = return_url;
    }

    public String getSign_type() {
        return sign_type;
    }

    public void setSign_type(String sign_type) {
        this.sign_type = sign_type;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getGatewayUrl() {
        return gatewayUrl;
    }

    public void setGatewayUrl(String gatewayUrl) {
        this.gatewayUrl = gatewayUrl;
    }

    public String getTime_out() {
        return time_out;
    }

    public void setTime_out(String time_out) {
        this.time_out = time_out;
    }
}
